package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.mapper.SkuMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.entity.Sku;
import cn.tedu.csmall.product.pojo.entity.Spu;
import cn.tedu.csmall.product.pojo.param.AlbumAddNewParam;
import cn.tedu.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.AlbumService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    private AlbumMapper mapper;
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SpuMapper spuMapper;

    @Override
    public void addNew(AlbumAddNewParam albumAddNewParam) {
        log.info("开始:【新增相册】");
        log.debug("传入参数,albumAddNewParam={}", albumAddNewParam);
        //检查相册名称是否被占用，如果被占用，则抛出异常
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",albumAddNewParam.getName());
        int countByName = mapper.selectCount(queryWrapper);
        log.info("规则：根据相册名称检索数据库结果,countByName={}", countByName);
        if(countByName > 0){
            log.info("结果2:不满足相册新增规则，抛出自定异常...");
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "添加相册失败，相册名称已经被占用！");
        }

        log.info("满足相册新增规则，开始新增...");
        //名称未占用，添加相册到数据库
        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewParam,album);
        album.setGmtCreate(LocalDateTime.now()).setGmtModified(LocalDateTime.now());
        /*album.setGmtCreate(LocalDateTime.now());
        album.setGmtModified(LocalDateTime.now());*/
        log.debug("新增相册数据,album={}", album);
        int rows = mapper.insert(album);
        if(rows != 1){
            String message = "添加相册失败，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", 1, rows);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:新增相册结果,rows={}", rows);
    }

    @Override
    public void deleteById(Long id) {
        log.info("开始:【删除相册】");
        log.debug("传入参数,id={}", id);
        //检查相册ID检查相册是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = mapper.selectCount(queryWrapper);
        log.info("规则：根据相册id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据相册id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "删除相册失败，相册数据不存在！");
        }

        //根据相册ID检查图片是否存在数据，如果存在，则抛出异常
        QueryWrapper<Picture> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("album_id", id);
        int countByAlbumId = pictureMapper.selectCount(queryWrapper2);
        log.info("规则：根据相册id检索图片数据库结果,countByAlbumId={}", countByAlbumId);
        if(countByAlbumId > 0){
            log.info("结果3:根据相册id检索图片数据量>0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "删除相册失败，相册内存在图片！");
        }

        //根据相册ID检查Sku是否存在数据，如果存在，则抛出异常
        QueryWrapper<Sku> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("album_id", id);
        int countByAlbumId3 = skuMapper.selectCount(queryWrapper3);
        log.info("规则：根据相册id检索sku数据库结果,countByAlbumId3={}", countByAlbumId3);
        if(countByAlbumId3 > 0){
            log.info("结果4:根据相册id检索sku数据量>0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "删除相册失败，sku内存在相册数据！");
        }

        //根据相册ID检查Spu是否存在数据，如果存在，则抛出异常
        QueryWrapper<Spu> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("album_id", id);
        int countByAlbumId4 = spuMapper.selectCount(queryWrapper4);
        log.info("规则：根据相册id检索spu数据库结果,countByAlbumId4={}", countByAlbumId4);
        if(countByAlbumId4 > 0){
            log.info("结果5:根据相册id检索spu数据量>0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "删除相册失败，spu内存在相册数据！");
        }

        int rows = mapper.deleteById(id);
        if(rows != 1){
            String message = "删除相册失败，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", 1, rows);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.info("结果1:删除相册结果,rows={}", rows);
    }

    @Override
    public void updateInfoById(Long id, AlbumUpdateInfoParam albumUpdateInfoParam) {
        log.info("开始:【根据id编辑相册】");
        log.debug("传入参数,id={},albumUpdateInfoParam={}", id, albumUpdateInfoParam);

        //检查相册ID检查相册是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Album> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", id);
        int countById = mapper.selectCount(queryWrapper2);
        log.info("规则：根据相册id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据相册id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "编辑相册失败，相册数据不存在！");
        }

        //检查相册名称是否被占用，如果被占用，则抛出异常
        QueryWrapper<Album> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("name",albumUpdateInfoParam.getName())
                .ne("id", id);
        int countByName = mapper.selectCount(queryWrapper3);
        log.info("规则：根据相册名称检索数据库结果,countByName={}", countByName);
        if(countByName > 0){
            log.info("结果3:不满足相册编辑规则，抛出自定异常...");
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "编辑相册失败，相册名称已经被占用！");
        }

        Album album = new Album();
        BeanUtils.copyProperties(albumUpdateInfoParam, album);
        album.setId(id);
        int rows = mapper.updateById(album);
        if(rows != 1){
            String message = "编辑相册失败，服务器忙，请稍后再试！";
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", 1, rows);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.info("结果1:根据id编辑相册结果,rows={}", rows);
    }

    @Override
    public AlbumStandardVO getStandardById(Long id) {
        log.info("开始:【根据ID查询相册】");
        log.debug("传入参数,id={}", id);
        AlbumStandardVO standardById = mapper.getStandardById(id);

        //根据相册ID检查相册是否存在数据，如果不存在，则抛出异常
        if(standardById==null){
            log.info("结果2:根据相册id查询详情，结果为null，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "查询相册详情失败，相册数据不存在");
        }

        log.info("结果1:查询出数据,standardById");
        return standardById;
    }

    @Override
    public PageData<AlbumListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum, pageSize);
    }

    @Override
    public PageData<AlbumListItemVO> list(Integer pageNum, Integer pageSize) {
        log.info("开始:【查询-相册-分页】");
        log.debug("传入参数, pageNum={}, pageSize={}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<AlbumListItemVO> list = mapper.list();
        PageInfo<AlbumListItemVO> pageInfo = new PageInfo<>(list);
        PageData<AlbumListItemVO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        log.debug("结果1: 查询结果：pageData={}", pageData);
        return pageData;
    }
}
